var net      = require('net');
var readline = require('readline');

var HOST     = '127.0.0.1';
var PORT     = 9001;

var client   = new net.Socket();

var dataid   = 0;

var con_msg = {
  mc: 'connected',
  id: 0,
  name: 'seven'
};

var say_msg = {
  mc: 'say',
  msg: 'hello'
}

client.connect(PORT, HOST, function() {
  console.log('CONNECTED TO: ' + HOST + ':' + PORT);
  // Write a message to the socket as soon as the client is connected, the server will receive it as message from the client 
  var tmsg = JSON.stringify(con_msg);
  client.write(tmsg);
});

// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function(data) {
  console.log('getMsg: ' + data);
});

// Add a 'close' event handler for the client socket
client.on('close', function() {
  console.log('Connection closed');
});


var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.on('line', function(cmd) {
  say_msg.msg = cmd;
  var tmsg = JSON.stringify(say_msg);
  client.write(tmsg);
});